<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="18">
          <el-form-item prop="fuzzy">
            <el-input
              v-model="searchForm.fuzzy"
              style="width: 250px"
              placeholder="仪器名称/仪器编号/仪器型号/仪器品牌"
            />
          </el-form-item>
          <el-form-item prop="type" label="仪器类别:">
            <el-select
              v-model="searchForm.type"
              style="width: 145px"
              placeholder="请选择仪器类别"
              clearable
            >
              <el-option
                v-for="item in typleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="classType" label="检测类别:">
            <el-select
              v-model="searchForm.classType"
              style="width: 145px"
              placeholder="请选择检测类别"
              clearable
            >
              <el-option
                v-for="item in classTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="iconfont icon-chaxun1"
              size="small"
              @click="handleSearch"
            >
              查询</el-button
            >
            <el-button type="primary" size="small" @click="instrumentData"
              >批量出库</el-button
            >
            <el-button type="primary" size="small" @click="instrumentVisible"
              >一键出库</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div>
      <el-table
        stripe
        border
        :data="factorData"
        class="table-item"
        highlight-current-row
        :header-cell-style="{ background: '#ECF3FC' }"
        style="width: 100%"
        @selection-change="handleselectionChange"
      >
        <template slot="empty">
          <div class="table_empty">
            <div class="empty_tips">
              <span> 暂无数据 </span>
            </div>
          </div>
        </template>
        <el-table-column
          type="selection"
          fixed="left"
          width="55"
          align="center"
        />
        <el-table-column type="index" width="70" align="center" label="序号" />
        <el-table-column
          prop="number"
          label="仪器编号"
          width="180"
          align="center"
        />
        <el-table-column
          prop="name"
          label="仪器名称"
          width="130"
          align="center"
        />
        <el-table-column
          prop="model"
          label="仪器型号"
          width="120"
          align="center"
        />
        <el-table-column
          prop="manufacturer"
          label="生产厂家"
          width="100"
          align="center"
        />
        <el-table-column
          prop="type"
          label="仪器类别"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.type == '1'">现场采样仪器</span>
            <span v-if="scope.row.type == '2'">现场检测仪器</span>
            <span v-if="scope.row.type == '3'">实验室仪器</span>
            <span v-if="scope.row.type == '4'">校准仪器</span>
            <span v-if="scope.row.type == '5'">样品制备仪器</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="classType"
          label="检测类别"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.classType == '1'"> 水</span>
            <span v-if="scope.row.classType == '2'">气</span>
            <span v-if="scope.row.classType == '3'">噪声</span>
            <span v-if="scope.row.classType == '4'"
              >土壤、底泥、固废、污泥和生活垃圾</span
            >
            <span v-if="scope.row.classType == '5'">样品制备</span>
            <span v-if="scope.row.classType == '6'">小型分析仪器</span>
            <span v-if="scope.row.classType == '7'">色谱仪 </span>
            <span v-if="scope.row.classType == '8'">光谱仪</span>
            <span v-if="scope.row.classType == '9'">质谱仪</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="position"
          label="放置地点"
          width="150"
          align="center"
        />
        <el-table-column
          prop="startDate"
          label="申领时间"
          width="140"
          align="center"
        >
        </el-table-column>

        <el-table-column
          prop="endDate"
          label="领用时间"
          width="180"
          align="center"
        />
        <el-table-column
          prop="startDate,endDate"
          label="预计使用日期"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.startDate }}~{{ scope.row.endDate }}
          </template>
        </el-table-column>
        <el-table-column
          prop="returnDate"
          label="应归还时间"
          width="120"
          align="center"
        />
        <el-table-column
          prop="applyUser"
          label="领用人"
          width="120"
          align="center"
        />
        <el-table-column
          prop="applyStatus"
          label="出入库状态"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <span
              v-if="scope.row.applyStatus == '1'"
              style="color: green; font-weight: bold"
              >在库</span
            >
            <span
              v-if="scope.row.applyStatus == '2'"
              style="color: orange; font-weight: bold"
              >待出库</span
            >
            <span
              v-if="scope.row.applyStatus == '3'"
              style="color: red; font-weight: bold"
              >已出库</span
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="190">
          <template slot-scope="scope">
            <el-button type="text" @click="View(scope.row)"> 查看</el-button>

            <div class="erc-divider-vertical" v-if="scope.row.applyStatus != 3"/>

            <template v-if="scope.row.applyStatus != 3">
              <el-button type="text" @click="change(scope.row)">
                更改</el-button
              >
            </template>

            <div class="erc-divider-vertical" v-if="scope.row.applyStatus != 3"/>

            <template v-if="scope.row.applyStatus != 3">
              <el-button type="text" @click="Instrument(scope.row)">
                出库</el-button
              >
            </template>

            <div class="erc-divider-vertical" />
            <el-button type="text" @click="delate(scope.row)"> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 出库弹框 -->
    <el-dialog
      :title="titletTit[customerDialog]"
      :visible.sync="transferDialogVisible"
      width="40%"
      center
      :close-on-click-modal="false"
    >
      <el-form
        ref="transferDialogData"
        :model="transferDialogData"
        label-width="150px"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="15">
            <el-form-item prop="name" label="项目名称:">
              <el-select
                style="width: 160%"
                v-model="transferDialog.name"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <el-form-item prop="createUserName" label="申领人:">
              <el-input
                v-model="transferDialog.createUserName"
                :disabled="true"
                style="width: 160%"
              />
            </el-form-item>
          </el-col>

          <el-col :span="15">
            <el-form-item prop="startDate" label="预计开始使用日期:">
              <el-date-picker
                :disabled="true"
                style="width: 160%"
                type="daterange"
                range-separator="至"
                value-format="yyyy-MM-dd"
                startDate-placeholder="开始日期"
                endDate-placeholder="结束日期"
                v-model="valueData"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="15">
            <el-form-item prop="applyUser" label="领用人:">
              <el-input
                :disabled="isLook"
                v-model="transferDialogData.applyUser"
                style="width: 160%"
                placeholder="请输入领用人"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="transferDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="Confirm()">确认</el-button>
      </div>
    </el-dialog>
    <!-- 更改 -->
    <el-dialog
      title="仪器申领更改"
      :visible.sync="changeVisible"
      width="950px"
      center
      :close-on-click-modal="false"
    >
      <el-form
        ref="claimchanges"
        :rules="rules"
        :model="claimchanges"
        label-width="140px"
      >
        <el-form-item prop="type" label="选择仪器:">
          <el-col :span="5">
            <el-select v-model="claimchanges.type" style="width: 150px">
              <el-option
                v-for="item in typleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-col>

          <el-col :span="5">
            <el-select v-model="claimchanges.className" style="width: 150px">
              <el-option
                v-for="item in classTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-col>

          <el-col :span="5">
            <el-select v-model="claimchanges.model" style="width: 150px">
              <el-option
                v-for="item in deviceName"
                :key="item.id"
                :label="item.deviceName"
                :value="item.deviceName"
              ></el-option>
            </el-select>
          </el-col>

          <el-col :span="5">
            <el-select v-model="claimchanges.numberName" style="width: 150px">
              <el-option
                v-for="item in number"
                :key="item.id"
                :label="item.number"
                :value="item.number"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="changeVisible = false">取消</el-button>
        <el-button type="primary" @click="ClaimchangesConfirm">确认</el-button>
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog
      title="查看"
      :visible.sync="MyInstrumentView"
      center
      top="5vh"
      width="1400px"
    >
      <div>
        <div class="install-title">出入库申请详情信息</div>
        <el-form label-width="130px">
          <el-row :gutter="12">
            <el-col :span="8">
              <el-form-item prop="number" label="仪器编号:">
                {{ ViewForm.number }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="name" label="仪器名称:">
                {{ ViewForm.name }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="model" label="仪器型号:">
                {{ ViewForm.model }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="manufacturer" label="生产厂家:">
                {{ ViewForm.manufacturer }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="type" label="仪器类别:">
                <el-form-item>
                  <span v-if="ViewForm.type == '1'">现场采样仪器</span>
                  <span v-if="ViewForm.type == '2'">现场检测仪器</span>
                  <span v-if="ViewForm.type == '3'">实验室仪器</span>
                  <span v-if="ViewForm.type == '4'">校准仪器</span>
                  <span v-if="ViewForm.type == '5'">样品制备仪器</span>
                </el-form-item>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="classType" label="检测类别:">
                <span v-if="ViewForm.classType == '1'"> 水</span>
                <span v-if="ViewForm.classType == '2'">气</span>
                <span v-if="ViewForm.classType == '3'">噪声</span>
                <span v-if="ViewForm.classType == '4'"
                  >土壤、底泥、固废、污泥和生活垃圾</span
                >
                <span v-if="ViewForm.classType == '5'">样品制备</span>
                <span v-if="ViewForm.classType == '6'">小型分析仪器</span>
                <span v-if="ViewForm.classType == '7'">色谱仪 </span>
                <span v-if="ViewForm.classType == '8'">光谱仪</span>
                <span v-if="ViewForm.classType == '9'">质谱仪</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="position" label="放置地点:">
                {{ ViewForm.position }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="申领时间:" prop="startDate">
                {{ ViewForm.startDate }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="endDate" label="领用时间:">
                {{ ViewForm.endDate }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="startDate," label="预计使用日期:">
                {{ ViewForm.startDate }}~{{ ViewForm.endDate }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="returnDate" label="应归还时间:">
                {{ ViewForm.returnDate }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="applyUser" label="领用人:">
                {{ ViewForm.applyUser }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="applyStatus" label="出入库状态:">
                <template>
                  <span
                    v-if="ViewForm.applyStatus == '1'"
                    style="color: green;font-weight: bold"
                    >在库</span
                  >
                  <span
                    v-if="ViewForm.applyStatus == '2'"
                    style="color: red;font-weight: bold"
                    >待出库
                  </span>
                  <span
                    v-if="ViewForm.applyStatus == '3'"
                    style="color: red;font-weight: bold"
                    >已出库
                  </span>
                </template>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>
    <div style="float: right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"
      />
    </div>
  </div>
</template>

<script>
import * as api from "@/api/Accesslist";
import { getUser } from "@/api/system/user";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
export default {
  components: { Pagination, getUser },
  filters: {},
  data() {
    return {
      valueData: "",
      limit: 5,
      total: 0,
      isLook: false,
      factorData: [], //数据
      transferDialogVisible: false,
      changeVisible: false,
      MyInstrumentView: false,
      ViewForm: [],
      modelOption: [],
      numberOption: [],
      rules: {
        applyUser: [
          { required: true, message: "领用人不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "仪器不能为空", trigger: "blur" }]
      },
      typleList: [
        {
          name: "现场采样仪器",
          id: 1
        },
        {
          name: "现场检测仪器",
          id: 2
        },
        {
          name: "实验室仪器",
          id: 3
        },
        {
          name: "校准仪器",
          id: 4
        }
      ],
      classTypeList: [
        {
          name: "水",
          id: 1
        },
        {
          name: "气",
          id: 2
        },
        {
          name: "噪声",
          id: 3
        },
        {
          name: "土壤、底泥、固废、污泥和生活垃圾",
          id: 4
        },
        {
          name: "样品制备",
          id: 5
        },
        {
          name: "小型分析仪器",
          id: 6
        },
        {
          name: "色谱仪",
          id: 7
        },
        {
          name: "光谱仪",
          id: 8
        },
        {
          name: "质谱仪",
          id: 9
        }
      ],
      searchForm: {
        pageNumber: 1,
        pageSize: 10
      },
      claimchanges: {
        subId: "",
        deviceId: "1",
        className: "",
        model: "",
        numberName: ""
      },
      transferDialog: {
        startDate: "",
        endDate: "",
        createUserName: ""
      },
      transferDialogData: {
        subId: "",
        applyUser: "",
        usdId: null
      },
      searchFormChange: {
        type: ""
      },
      issueApply: [],
      className: [],
      deviceName: [],
      number: [],
      multipleselection: [],
      selection: [],
      titletTit: {
        edit: "出库",
        create: "批量出库",
        oneClick: "一键出库"
      },
      customerDialog: "create"
    };
  },
  created() {
    this.handleSearch();
  },
  methods: {
    handleSearch() {
      //   console.log(123);
      this.searchForm.id = this.$route.params.id;
      api.issueApplyDetail(this.searchForm).then(res => {
        // console.log(res);
        if (res.code === 200) {
          this.factorData = res.result.records;
          // console.log(this.factorData);
          this.total = res.result.total;
          // console.log(this.factorData);
        }
      });
    },

    //出库弹框
    Instrument(row) {
      console.log(row);
      this.customerDialog = "edit";
      getUser(this.$store.getters.userInfo.userId).then(response => {
        this.transferDialog.createUserName = response.result.user.empName;
      });
      this.transferDialogVisible = true;
      this.transferDialog.name = row.name;
      this.transferDialog.createUserName = row.createUserName;
      this.transferDialogData.subId = row.id;
      var arr = [];
      arr.push(row.startDate);
      arr.push(row.endDate);
      this.valueData = arr;
    },
    // 获取列表勾中id
    handleselectionChange(val) {
      console.log(val);
      this.multipleselection = val;
      var arr = [];
      for (var item of val) {
        // console.log(item.id);
        arr.push(item.id);
        // console.log(arr + []);
        this.selection = arr;
        console.log(this.selection);
      }
    },
    // 批量出库弹框
    instrumentData() {
      this.customerDialog = "create";
      getUser(this.$store.getters.userInfo.userId).then(response => {
        this.transferDialog.createUserName = response.result.user.empName;
      });
      if (this.multipleselection && this.multipleselection.length === 0) {
        this.$message.warning("请选择仪器");
      } else {
        this.transferDialogVisible = true;
        this.transferDialog.name = this.multipleselection[0].name;
        console.log(this.transferDialog.name);
        this.transferDialog.createUserName = this.multipleselection[0].createUserName;
        console.log(this.transferDialog.createUserName);
        var arr = [];
        arr.push(this.multipleselection[0].startDate);
        arr.push(this.multipleselection[0].endDate);
        this.valueData = arr;
        this.transferDialogData.subId = this.selection;
        console.log(this.transferDialogData.subId);
      }
    },
    // 一键出库弹框
    instrumentVisible() {
      this.customerDialog = "oneClick";
      getUser(this.$store.getters.userInfo.userId).then(response => {
        this.transferDialog.createUserName = response.result.user.empName;
      });
      this.transferDialogVisible = true;
      this.transferDialogData.subId = this.selection;
      console.log(this.transferDialogData.subId);
    },
    //出库
    Confirm() {
      this.$refs.transferDialogData.validate(valid => {
        if (valid) {
          api
            .assign(this.transferDialogData)
            .then(request => {
              this.$notify({
                title: "成功",
                message: request.message,
                type: "success"
              });
              this.handleSearch();
              this.transferDialogVisible = false;
            })
            .catch(() => {});
        } else {
          return false;
        }
      });
    },
    //查询设备树
    change(row) {
      console.log(row);
      this.changeVisible = true;
      this.searchFormChange.type = row.type;
      console.log(this.searchFormChange.type);
      this.claimchanges.subId = row.id;
      console.log(this.claimchanges.subId);
      api.deviceTree(this.searchFormChange).then(res => {
        console.log(res);
        if (res.code === 200) {
          this.issueApply = res.result;
          console.log(this.issueApply);
          this.className = this.issueApply[0].className;
          console.log(this.className);
          this.deviceName = this.issueApply[0].deviceNameVos;
          console.log(this.deviceName);
          this.number = this.issueApply[0].deviceNameVos[0].numberVos;
          this.claimchanges.deviceId = this.issueApply[0].deviceNameVos[0].numberVos[0].deviceInfoVo.id;
          console.log(this.claimchanges.deviceId);
        }
      });
    },
    // 仪器申领更改
    ClaimchangesConfirm() {
      this.$refs.claimchanges.validate(valid => {
        if (valid) {
          api
            .changeDevice(this.claimchanges)
            .then(request => {
              this.$notify({
                title: "成功",
                message: request.message,
                type: "success"
              });
              this.handleSearch();
              this.transferDialogVisible = false;
            })
            .catch(() => {});
        } else {
          return false;
        }
      });
    },
    delate(val) {
      console.log(val);
    },
    // 查看
    View(row) {
      console.log(row);
      this.MyInstrumentView = true;
      this.ViewForm = row;
    }
  }
};
</script>

<style scoped lang="scss">
.clearfix {
  line-height: 40px;
  display: flex;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.clearfix {
  line-height: 40px;
  display: flex;
}
.install-title {
  display: flex;
  align-items: center;
  line-height: 40px;
  padding-left: 20px;
  color: rgb(15, 14, 14);
  background-color: #d7dae2;
  justify-content: flex-start;
  margin-top: 15px;
}
.delete {
  width: 10px;
  height: 10px;
  position: relative;
  top: -10px;
  cursor: pointer;
}
::v-deep .el-upload-list {
  width: 100px;
  display: flex;
  margin: 10px 10px;
}
::v-deep .el-upload-list > li {
  margin: 10px 10px;
}
::v-deep.el-upload-list__item {
  margin: 10px 10px;
}
::v-deep.el-upload-list--picture .el-upload-list__item {
  margin: 10px 10px !important;
}
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}

::v-deep.avatar-upload-preview {
  width: 400px;
  border-radius: 0%;
  height: 400px;
  > img {
    transform: translateX(0);
  }
}
.user-info-head:hover:after {
  content: "+";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  border-radius: 50%;
}
</style>
